<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能体聊天测试</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .chat-container {
            border: 1px solid #ddd;
            border-radius: 8px;
            height: 400px;
            overflow-y: auto;
            padding: 15px;
            margin-bottom: 20px;
            background-color: #fafafa;
        }
        .message {
            margin-bottom: 15px;
            padding: 10px;
            border-radius: 8px;
            max-width: 80%;
        }
        .user-message {
            background-color: #007bff;
            color: white;
            margin-left: auto;
            text-align: right;
        }
        .agent-message {
            background-color: #e9ecef;
            color: #333;
            margin-right: auto;
        }
        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .input-container {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        #messageInput {
            flex: 1;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 16px;
        }
        button {
            padding: 12px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #0056b3;
        }
        button:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .test-buttons {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 10px;
            margin-bottom: 20px;
        }
        .test-button {
            background-color: #28a745;
            padding: 10px;
            font-size: 14px;
        }
        .test-button:hover {
            background-color: #218838;
        }
        .loading {
            display: none;
            text-align: center;
            color: #666;
            font-style: italic;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
            padding: 10px;
            border-radius: 6px;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🤖 智能体聊天测试</h1>
        
        <div class="info">
            <strong>智能体功能:</strong>
            <ul>
                <li>🔍 网络搜索 - 获取实时信息</li>
                <li>⏰ 时间查询 - 获取当前时间</li>
                <li>🧮 数学计算 - 计算数学表达式</li>
                <li>🌤️ 天气查询 - 查询城市天气</li>
                <li>💬 对话上下文 - 获取历史对话</li>
            </ul>
        </div>
        
        <div class="test-buttons">
            <button class="test-button" onclick="testQuestion('你好，请介绍一下你的功能')">基本介绍</button>
            <button class="test-button" onclick="testQuestion('现在几点了？')">时间查询</button>
            <button class="test-button" onclick="testQuestion('帮我计算 123 + 456 * 2')">数学计算</button>
            <button class="test-button" onclick="testQuestion('北京今天天气怎么样？')">天气查询</button>
            <button class="test-button" onclick="testQuestion('最新的人工智能发展趋势是什么？')">网络搜索</button>
            <button class="test-button" onclick="testQuestion('请告诉我现在的时间，然后搜索一下今天有什么重要新闻')">复合查询</button>
        </div>
        
        <div class="chat-container" id="chatContainer">
            <div class="message agent-message">
                👋 你好！我是智能体助手，我可以帮你搜索信息、计算数学题、查询时间和天气等。试试上面的测试按钮或者直接输入问题吧！
            </div>
        </div>
        
        <div class="input-container">
            <input type="text" id="messageInput" placeholder="输入你的问题..." onkeypress="handleKeyPress(event)">
            <button onclick="sendMessage()" id="sendButton">发送</button>
        </div>
        
        <div class="loading" id="loading">🤔 智能体正在思考中...</div>
    </div>

    <script>
        const chatContainer = document.getElementById('chatContainer');
        const messageInput = document.getElementById('messageInput');
        const sendButton = document.getElementById('sendButton');
        const loading = document.getElementById('loading');
        
        let conversationId = Math.floor(Math.random() * 1000) + 1;
        let userId = 1;
        
        function addMessage(content, isUser = false, isError = false) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${isUser ? 'user-message' : (isError ? 'error-message' : 'agent-message')}`;
            messageDiv.textContent = content;
            chatContainer.appendChild(messageDiv);
            chatContainer.scrollTop = chatContainer.scrollHeight;
        }
        
        function showLoading(show) {
            loading.style.display = show ? 'block' : 'none';
            sendButton.disabled = show;
            messageInput.disabled = show;
        }
        
        async function sendMessage(message = null) {
            const query = message || messageInput.value.trim();
            if (!query) return;
            
            // 添加用户消息
            addMessage(query, true);
            
            // 清空输入框
            if (!message) {
                messageInput.value = '';
            }
            
            // 显示加载状态
            showLoading(true);
            
            try {
                const response = await fetch('/api/v1/chat/agent', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        message: query,
                        conversation_id: conversationId,
                        user_id: userId
                    })
                });
                
                if (!response.ok) {
                    throw new Error(`HTTP ${response.status}: ${response.statusText}`);
                }
                
                const data = await response.json();
                
                if (data.error) {
                    addMessage(`❌ ${data.error}`, false, true);
                } else {
                    addMessage(`🤖 ${data.message}`);
                }
                
            } catch (error) {
                console.error('发送消息失败:', error);
                addMessage(`❌ 网络错误: ${error.message}`, false, true);
            } finally {
                showLoading(false);
            }
        }
        
        function testQuestion(question) {
            sendMessage(question);
        }
        
        function handleKeyPress(event) {
            if (event.key === 'Enter' && !event.shiftKey) {
                event.preventDefault();
                sendMessage();
            }
        }
        
        // 页面加载完成后聚焦输入框
        window.addEventListener('load', () => {
            messageInput.focus();
        });
    </script>
</body>
</html>